/**
 * 
 */
package com.wzl.qs;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.wzl.qs.db.DbAdapter;
import com.wzl.qs.db.ENote;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Bitmap;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * @author Wu Wei
 * 
 */
public class AddNote extends Activity {

	private static final int ABOUT = 1;
	private static final int HELP = 2;

	EditText etName;
	EditText etDescription;

	Button btnTakePhoto;
	ImageView ivPhoto;
	Button btnRecordAudio;
	Button btnStopRecord;
	Button btnPlayAudio;
	Button btnAddNote;

	MediaRecorder recorder = null;
	String audioFilePath = null;
	String photoPath = null;
	String thumberPath = null;
	MediaPlayer mp = null;

	Context context;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add);

		context = this;

		etName = (EditText) findViewById(R.id.etName);
		etDescription = (EditText) findViewById(R.id.etText);

		ivPhoto = (ImageView) findViewById(R.id.ivImage);
		btnTakePhoto = (Button) findViewById(R.id.btnTakePhoto);
		btnTakePhoto.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClassName("com.wzl.qs", "com.wzl.qs.TakePhoto");
				SimpleDateFormat timeStampFormat = new SimpleDateFormat(
						"yyyyMMddHHmmssSS");
				photoPath = "/sdcard/pic" + timeStampFormat.format(new Date())
						+ ".jpg";
				thumberPath = "/sdcard/pic"
						+ timeStampFormat.format(new Date()) + "_s.jpg";
				intent.putExtra("fileName", photoPath);
				intent.putExtra("thumber", thumberPath);
				TakePhoto.hasTaken = false;
				startActivityForResult(intent, 1);
			}
		});

		btnRecordAudio = (Button) findViewById(R.id.btnRecordAudio);
		btnRecordAudio.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				recorder = new MediaRecorder();
				SimpleDateFormat timeStampFormat = new SimpleDateFormat(
						"yyyyMMddHHmmssSS");
				String filename = "sound" + timeStampFormat.format(new Date())
						+ ".3gpp";
				audioFilePath = "/sdcard/" + filename;
				recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
				recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
				recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
				recorder.setOutputFile(audioFilePath);
				try {
					recorder.prepare();
					recorder.start();
					btnRecordAudio.setText("");
//					btnRecordAudio.setTextColor(0xff0000);
//					btnRecordAudio.setle
				} catch (IllegalStateException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});

		btnStopRecord = (Button) findViewById(R.id.btnStopRecard);
		btnStopRecord.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (recorder != null) {
					recorder.stop();
					recorder.reset();
					recorder.release();
					btnRecordAudio.setText("Record");
				}
			}
		});

		btnPlayAudio = (Button) findViewById(R.id.btnPlayAudio);
		btnPlayAudio.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (audioFilePath != null) {
					mp = new MediaPlayer();
					try {
						mp.setDataSource(audioFilePath);
						mp.prepare();
						mp.start();
					} catch (IllegalArgumentException e1) {
						e1.printStackTrace();
					} catch (IllegalStateException e1) {
						e1.printStackTrace();
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			}
		});

		btnAddNote = (Button) findViewById(R.id.btnAddNote);
		btnAddNote.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				/*
				 * get the fields new a enote add the note
				 */
				String _name = etName.getText().toString();
				String _photopath;
				if (photoPath == null) {
					_photopath = "";
				} else {
					_photopath = photoPath;
				}
				String _audioPath;
				if (audioFilePath == null) {
					_audioPath = "";
				} else {
					_audioPath = audioFilePath;
				}
				String _thumbernail;
				if (thumberPath == null) {
					_thumbernail = "";
				} else {
					_thumbernail = thumberPath;
				}
				String _videopath = "";
				String _description = etDescription.getText().toString();
				String _inputdate = DateTimeUtils.getToday();
				String _inputtime = DateTimeUtils.getNow();
				int _popularity = 0;
				ENote note = new ENote();
				note.setName(_name);
				note.setPhotoPath(_photopath);
				note.setAudioPath(_audioPath);
				note.setVideoPath(_videopath);
				note.setDescription(_description);
				note.setInputDate(_inputdate);
				note.setInputTime(_inputtime);
				note.setPopularity(_popularity);
				note.setThumberNail(_thumbernail);
				DbAdapter dba = new DbAdapter(context);
				dba.open();
				dba.AddENote(note);
				dba.close();

				photoPath = null;
				thumberPath = null;
				audioFilePath = null;
				ivPhoto.setImageBitmap(null);

				Toast.makeText(context, "Your encourage note has been saved.",
						Toast.LENGTH_SHORT).show();

			}
		});

	}

	protected void onActivityResult(int requestCode, int resultCode,
			Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		if (requestCode == 1) {
			Bundle data = intent.getExtras();
			String currentPhotoFile = data.getString("fileName");
			loadImage(thumberPath);
		}
	}

	private void loadImage(String filename) {
		if (TakePhoto.hasTaken) {
			try {
				Bitmap bm = BitmapUtil.getBitmap(filename, 180);
				ivPhoto.setImageBitmap(bm);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuItem mi = menu.add(Menu.NONE, ABOUT, Menu.NONE, "About");
		mi.setIcon(R.drawable.qsm);
		MenuItem mihelp = menu.add(Menu.NONE, HELP, Menu.NONE, "Help");
		mihelp.setIcon(android.R.drawable.ic_dialog_info);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int itemid = item.getItemId();
		switch (itemid) {
		case ABOUT:
			Intent i = new Intent(this, About.class);
			startActivity(i);
			break;

		case HELP:
			Intent aboutIntent = new Intent(this, Help2.class);
			startActivity(aboutIntent);
			break;

		}
		return super.onOptionsItemSelected(item);
	}

}
